package pers.gxm.composite;

import java.util.ArrayList;
import java.util.Iterator;

public  class Directory extends Entry {

    private String name;

    private ArrayList<Entry> directory = new ArrayList<>();

    public Directory(String name){
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getSize() {
        int size = 0;
        Iterator<Entry> iterator = directory.iterator();
        while(iterator.hasNext()){
            Entry next = iterator.next();
            size += next.getSize();
        }

        return size;
    }

    @Override
    public Entry add(Entry entry) throws FileTreatmentException {
        entry.parent = this;
        directory.add(entry);
//        个人答案部分
        entry.setCurrentPath(currentPath + "/" + this.name);
//        个人答案停止
        return this;
    }

    @Override
    protected void printList(String prefix) {
        System.out.println(prefix + "/" + this);
        Iterator<Entry> iterator = directory.iterator();
        while(iterator.hasNext()){
            Entry next = iterator.next();
            next.printList(prefix + "/" + name);
        }

    }


}
